package main

import (
	"fmt"
	"math/Rand"
	"time"

	"net/http"
	"os"
	"strings"
)

type LogCode struct {
	Ip     string
	Status int
}



func Index(w http.ResponseWriter, r *http.Request) {
	//为HTTPServer 添加 0-2 秒的随机延时；
	rand.Seed(time.Now().UnixNano())
	times := rand.Intn(2000)
	time.Sleep(time.Millisecond * time.Duration(times))

	header := r.Header
	fmt.Fprintln(w, "打印Header参数列表：")
	if len(r.Header) > 0 {
		for k, v := range header {
			fmt.Fprintf(w, "%s=%s\n", k, v[0])
		}
	}
	fmt.Fprintln(w, "\n环境变量VERSION")
	PATH := os.Getenv("PATH")
	fmt.Fprintf(w, "PATH:%s\n", PATH)
	//fmt.Fprintf(w,"GOPATH:%s\n",os.Getenv("GOPATH"))
	//fmt.Fprintf(w,"GOROOT:%s\n",os.Getenv("GOROOT"))
	//fmt.Fprintf(w,"JAVA_HOME:%s\n",os.Getenv("JAVA_HOME"))

	result := strings.Split(r.RemoteAddr, ":")
	logcode := LogCode{
		Ip:     result[0],
		Status: http.StatusOK,
	}

	fmt.Println("访客ip:", logcode.Ip)
	fmt.Println("访客状态:", logcode.Status)
}

func main() {
	http.HandleFunc("/", Index)
	http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
		//fmt.Fprintln(w, "StateCode:",http.StatusOK)
		fmt.Fprintln(w, http.StatusOK)
	})
	err := http.ListenAndServe(":8080", nil)
	// server := http.Server{
	// 	Addr:    "localhost:8000",
	// 	Handler: nil,
	// }
	// err := server.ListenAndServe()
	if err != nil {
		panic(err)
	}
}
